package com.ydl.shopping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydl.shopping.domain.TbGoods;
import com.ydl.shopping.domain.TbShoppingCart;
import com.ydl.shopping.exception.ServiceException;
import com.ydl.shopping.mapper.TbShoppingCartMapper;
import com.ydl.shopping.service.ITbGoodsService;
import com.ydl.shopping.service.ITbShoppingCartService;
import com.ydl.shopping.utils.SecurityUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TbShoppingCartServiceImpl extends ServiceImpl<TbShoppingCartMapper, TbShoppingCart> implements ITbShoppingCartService {
    @Resource
    private ITbGoodsService goodsService;

    @Resource
    private TbShoppingCartMapper shoppingCartMapper;

    @Override
    public QueryWrapper<TbShoppingCart> queryWrapper(TbShoppingCart entity) {
        QueryWrapper<TbShoppingCart> queryWrapper = new QueryWrapper<>();
        Long userId = entity.getUserId();
        if (userId != null) {
            queryWrapper.eq("user_id", userId);
        }
        Long goodsId = entity.getGoodsId();
        if (goodsId != null) {
            queryWrapper.eq("goods_id", goodsId);
        }

        return queryWrapper;
    }

    @Override
    public TbShoppingCart queryById(Long id) {
        return shoppingCartMapper.queryById(id);
    }

    @Override
    public List<TbShoppingCart> queryList(TbShoppingCart entity) {
        return shoppingCartMapper.queryList(entity);
    }

    @Override
    public boolean addCart(TbShoppingCart entity) {
        // 1. 获取用户ID
        Long userId = entity.getUserId();
        // 2. 获取商品ID
        Long goodsId = entity.getGoodsId();
        // 2.1 判断商品是否存在
        checkGoods(goodsId);

        // 3. 获取加入购物车数量
        Integer quantity = entity.getQuantity();
        // 4. 加入购物车
        TbShoppingCart shoppingCart = new TbShoppingCart(userId, goodsId, quantity);

        shoppingCart.setCreateBy(SecurityUtil.getUserName());
        boolean exitsCart = exitsCart(userId, goodsId);
        if (exitsCart) {
            return true;
        }
        return save(shoppingCart);
    }

    @Override
    public boolean updateCart(TbShoppingCart entity) {
        Long goodsId = entity.getGoodsId();
        if (goodsId != null) {
            checkGoods(goodsId);
        }
        entity.setUpdateBy(SecurityUtil.getUserName());
        return updateById(entity);
    }

    @Override
    public boolean exitsCart(Long userId, Long goodsId) {
        return query().eq("user_id", userId).eq("goods_id", goodsId).count() > 0;
    }

    private void checkGoods(Long goodsId) {
        // 2.1 判断商品是否存在
        TbGoods goods = goodsService.getById(goodsId);
        if (goods == null) {
            throw new ServiceException("商品不存在");
        }
        // 2.2 判断商品是否可用
        Integer status = goods.getStatus();
        if (status.equals(0)) {
            throw new ServiceException("商品不可用");
        }
        // 2.3 判断商品是否上架
        Integer isUp = goods.getIsUp();
        if (isUp.equals(0)) {
            throw new ServiceException("商品未上架");
        }
    }
}
